#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct QueNode
{
    int val;
    struct QueNode* next;
}Qnode;

typedef struct {
    Qnode* phead;
    Qnode* ptail;
    int size;
    int max;
} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k) {
    MyCircularQueue* mcq = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    if (mcq == NULL)
    {
        perror("malloc");
        exit(1);
    }
    Qnode* pcur = NULL;
    mcq->max = k;
    mcq->phead = mcq->ptail = NULL;
    while (k--)
    {
        Qnode* newnode = (Qnode*)malloc(sizeof(Qnode));
        if (mcq->phead == NULL)
        {
            pcur = mcq->phead = mcq->ptail = newnode;
        }
        else
        {
            pcur->next = newnode;
            pcur = newnode;
        }
    }
    pcur->next = mcq->phead;
    mcq->size = 0;
    return mcq;
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    return obj->size == 0;
}

bool myCircularQueueIsFull(MyCircularQueue* obj) {
    return obj->size == obj->max;
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    if (myCircularQueueIsFull(obj))
    {
        return false;
    }
    obj->ptail->val = value;
    obj->ptail = obj->ptail->next;
    obj->size++;

    return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj))
    {
        return false;
    }
    obj->phead = obj->phead->next;
    obj->size--;

    return true;
}

int myCircularQueueFront(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj))
    {
        return -1;
    }
    return obj->phead->val;
}

int myCircularQueueRear(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj))
    {
        return -1;
    }
    Qnode* pret = obj->phead;
    for (int i = 0; i < obj->size - 1; i++)
    {
        pret = pret->next;
    }
    return pret->val;
}

void myCircularQueueFree(MyCircularQueue* obj) {
    Qnode* pcur = obj->phead;
    Qnode* pnext;

    while (obj->max--)
    {
        pnext = pcur->next;
        free(pcur);
        pcur = pnext;
    }
    free(obj);
}

int main()
{
    MyCircularQueue* obj = myCircularQueueCreate(3);

    return 0;
}